<?php

/**
 * Class UsersController
 */
class UsersController extends AppController {

	/**
	 * Controller name
	 *
	 * @type string
	 */
	public $name = 'Users';

	/**
	 * Model
	 *
	 * @type array
	 */
	public $uses = ['User'];

	/**
	 * Component
	 *
	 * @type array
	 */
	public $components = ['Paginator'];

	/**
	 * Helper
	 *
	 * @type array
	 */
	public $helpers = ['Form', 'Paginator'];

	/**
	 * Content title
	 *
	 * @type string
	 */
	protected $metaTitle = '用户管理';

	/**
	 * Initialization
	 *
	 * @return void
	 */
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('register', 'login');
	}

	public function index() {
	}

	public function add() {
	}

	public function edit() {
	}

	public function delete() {
	}

	public function view() {
	}

	public function setting() {
	}

	public function profile() {
	}

	/**
	 * User login
	 *
	 * @return void
	 */
	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login($this->request->data)) {
				if (isset($data['remember']) && !empty($data['remember'])) {
					$cookie = [];
					$cookie['email'] = $data['email'];
					$cookie['password'] = $this->Auth->password($cookie['password']);
//					$this->Cookie->write('Auth.User', $cookie, true);
				}
				$this->success('登录成功', '/indexs/index');
			}
			$this->error('登录失败');
		}
		$this->layout = 'login';
	}

	/**
	 * User logout
	 *
	 * @return void
	 */
	public function logout() {
		$this->Session->read('Auth.User');
		$this->redirect($this->Auth->logout());
	}

	/**
	 * User register
	 *
	 * @return void
	 */
	public function register() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->success('注册成功');
			}
			$this->error('注册失败');
		}
		$this->layout = 'login';
	}
}
